package org.andcool.animation;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * @author Joosun
 * @since 2011-6-24
 */
public class Rotate3DAnimation extends Animation
{
	protected Camera mCamera;

	protected float mFromDegreesX;
	protected float mToDegreesX;

	protected float mFromDegreesY;
	protected float mToDegreesY;

	protected float mFromDegreesZ;
	protected float mToDegreesZ;

	protected float mCenterX;
	protected float mCenterY;
	protected float mCenterZ;

	public Rotate3DAnimation(final float pFromDegreesX, final float pToDegreesX, final float pFromDegreesY, final float pToDegreesY, final float pFromDegreesZ, final float pToDegreesZ, final float pCenterX, final float pCenterY, final float pCenterZ)
	{
		super();

		this.mFromDegreesX = pFromDegreesX;
		this.mToDegreesX = pToDegreesX;

		this.mFromDegreesY = pFromDegreesY;
		this.mToDegreesY = pToDegreesY;

		this.mFromDegreesZ = pFromDegreesZ;
		this.mToDegreesZ = pToDegreesZ;

		this.mCenterX = pCenterX;
		this.mCenterY = pCenterY;
		this.mCenterZ = pCenterZ;

		this.mCamera = new Camera();
	}

	@Override
	protected void applyTransformation(final float pInterpolatedTime, final Transformation pTransformation)
	{
		final Matrix matrix = pTransformation.getMatrix();

		this.mCamera.save();

		this.mCamera.translate(0, 0, this.mCenterZ);

		this.mCamera.rotateX(this.mFromDegreesX + (this.mToDegreesX - this.mFromDegreesX) * pInterpolatedTime);
		this.mCamera.rotateY(this.mFromDegreesY + (this.mToDegreesY - this.mFromDegreesY) * pInterpolatedTime);
		this.mCamera.rotateZ(this.mFromDegreesZ + (this.mToDegreesZ - this.mFromDegreesZ) * pInterpolatedTime);

		this.mCamera.translate(0, 0, -this.mCenterZ);

		this.mCamera.getMatrix(matrix);

		this.mCamera.restore();

		matrix.preTranslate(-this.mCenterX, -this.mCenterY);
		matrix.postTranslate(this.mCenterX, this.mCenterY);
	}
}